/* written by Jonathan Odom and Aneesh Butani
 * 
 * Some of the string literals have been left in this code,
 * simply because this parser class will be completely redesigned
 * if not replaced within the next few days.  For the final
 * submission, the code will meet all of the required c++ conventions.
 */

#include "XMLParser.h"
#include "Appt.h"


string XMLParser::APPT = "appt";
string XMLParser::DAY = "day";
string XMLParser::TIME = "time";
string XMLParser::SUBJECT = "subject";

XMLParser::XMLParser()
{
}

XMLParser::~XMLParser()
{
}

XMLParser::XMLParser(const string& fileName)
: myFileName(fileName)
{
}

XMLParser::XMLParser(const string& fileName, int whichEvent)
: myFileName(fileName)
{
//	ifstream f(fileName.c_str());
//	string tag = getTag(f);
//	if (tag.rfind("appt", 0)!=string::npos)
//		whichEvent=1;
//	if (tag.rfind("EVENTS", 0)!=string::npos)
//		whichEvent=2;
//	if (tag.rfind("Movies", 0)!=string::npos)
//		whichEvent=3;
//	f.close();
	
//	if(whichEvent == 1)
//	{
//	 	Appt ob;
//	 	
//	}
//	else if(whichEvent == 2)
//	{
//		
//	}
//	else if (whichEvent ==3)
//	{
//	}
//	else if(whichEvent ==3)
//	{
//		readMovies();
//	}
Appt ob;
if (whichEvent==1)
{	
	cout<<"blah";
	ob.readAppt(fileName, myEvents);
	cout<<"blah";
}
	else XMLParser(fileName);
	
}

//void XMLParser::readAppt()
//{
//	//Maybe make a readNodeAppt method?
//	//then just make a new readNode for each input
//	//Something better then if statements in read()?
//	
//	//Why is this not working ???
//	ifstream f(myFileName.c_str());
//	string node;
//	string temp;
//	while(!f.eof())
//	{
//		//getline does not include delimiter
//		getline(f,temp, '<');
//		getline(f,node, '>');
//		if(node.at(1)!='!')
//		{
//			readNodeAppt(node);
//		}
//		
//	}
//	
//}
//
//void XMLParser::readNodeAppt(const string& node)
//{
//	string tempDay = getInfoAppt(node,DAY);
//	string tempSubject = getInfoAppt(node,SUBJECT);
//	int range[2]; 
//	getTimeRangeAppt(node, TIME, range);
////	Appt cur* =new Appt(atoi(tempDay.c_str()),range[0], range[1],tempSubject);
////	Appt cur(atoi(tempDay.c_str()),range[0], range[1],tempSubject);
//	myEvents.push_back(new Appt(atoi(tempDay.c_str()),range[0], range[1],tempSubject));
//	//cout<<range[0]<<" to "<<range[1]<<endl;
//	
//}
//
//string XMLParser::getInfoAppt(const string& node, const string& infoType)
//{
//	unsigned int loc = node.find(infoType,0);
//	if(loc != string::npos)
//	{
//		int start = node.find('\"', loc);
//		int end = node.find('\"', start+1);
//		return(node.substr(start+1,end-start-1));
//	}
//	return "Type not found";
//}
//
//void XMLParser::getTimeRangeAppt(const string& node, const string& infoType, int range[])
//{
//	range[0] = 0;
//	range[1] = 0;
//
//	unsigned int loc = node.find(infoType,0);
//	if(loc != string::npos)
//	{
//		int start = node.find("\"",loc);
//		int end = node.find("\"", start + 1);
//		seperateTime(range, node.substr(start+1,end-start-1));
//		
//	}
//}
//
//void XMLParser::seperateTime(int range[], const string& times)
//{
//	unsigned int loc = times.find(".",0);
//	unsigned int loc2 = times.find(",",0);
//	if(loc != string::npos)
//	{
//		string temp = times.substr(0, loc);
//		range[0]= atoi(temp.c_str());
//		temp = times.substr(loc+1,times.length());
//		range[1] = atoi(temp.c_str());
//	}
//	else if(loc2 != string::npos)
//	{
//		string temp = times.substr(0, loc2-1);
//		range[0]= atoi(temp.c_str());
//		temp = times.substr(loc2+1,times.length());
//		range[1] = atoi(temp.c_str());
//	}
//		//cout<<range[0]<<" to "<<range[1]<<endl;
//}
//

vector<Event*> XMLParser::getEvents()
{
	return myEvents;
}

///*vector<Event> XMLParser::getEvents(const string& filter)
//{
//	if (filter=="")
//		return getEvents();
//	else {
//		vector<Event> filteredEvents;
//		for(unsigned int i = 0; i<myEvents.size(); i++)
//			if(myEvents[i].getDescription()==filter)
//				filteredEvents.push_back(myEvents[i]);
//		return filteredEvents;
//	}
//}*/
//
//
////methods for birthday, we know this should be done through inheritance,
////but the structure would not make sense so we are doing this and
////will refactor later into an organized structure
//
////void XMLParser::readBirthday()
////{
////	APPT = "EVENT";
////	DAY = "DATE";
////	SUBJECT = "DESCRIPTION";
////	ifstream f(myFileName.c_str());
////	string node;
////	string temp;
////	while(!f.eof())
////	{
////		getline does not include delimiter
////		getline(f,temp, '<');
////		getline(f,node, '>');
////		if(node.at(1)!='!')
////		{
////			readNodeBirthday(node);
////		}
////		
////	}
////	
////}
//
////void XMLParser::readNodeBirthday(const string& node)
////{
////	string tempDay = getInfoAppt(node,DAY);
////	string tempSubject = getInfoAppt(node,SUBJECT);
////	Event cur(atoi(tempDay.c_str()), tempSubject);
////	myEvents.push_back(cur);
////	
////}
//
////void XMLParser::getNode (ifstream& f, ofstream& f2, string event, string subevent="VOODOOCRAP")
////{
////	string endevent="/"+event;
////	string endsubevent="/"+subevent;
////	string tag, data, dump;
////	string description;
////	int date;
////	tag=gettag(f);
////	if (tag==event || tag.rfind(event,0) != string::npos)
////	{
////		do
////		{
////			tag=gettag(f);
////			if (tag==subevent)
////				{
////					f2<<tag<<":"<<endl;
////					do
////					{
////						tag=gettag(f);
////						getline (f,data,'<');
////						if (tag!=endsubevent)
////							f2<<tag<<": "<<data<<endl;		
////					}while (tag!=endsubevent);
////				}
////					if (tag==endsubevent)
////					{	getline (f,tag,'>');
////						getline (f,data,'<');
////					}
////					else
////						getline (f,data,'<');
////					
////					if (tag!=endevent && tag!=endsubevent)
////						{
////							if (tag=="DATE")
////								date=atoi(data.c_str());
////							if (tag=="DESCRIPTION")
////								description=data;
////							f2<<tag<<": "<<data<<endl;	
////							Event cur(date, description);
////							myEvents.push_back(cur);
////						}
////			}while (tag!=endevent);	
////		f2<<endl;
////	}
////}
//	
string XMLParser::getTag (ifstream& f)
{
	string tag;
	getline(f,tag,'<');
	getline (f,tag,'>');
	return tag;
}

//void XMLParser::readCeleb()
//{
//	//map<string,string> m;
//	ofstream f2("celebs.txt");
//	string event="EVENT";
//	string endevent="/"+event;
//	ifstream f(myFileName.c_str());
//	string tag, data, description;
//	int date;
//	
//		while (!f.eof())
//	{
//		//getNode(f,f2,event);
//		tag=gettag(f);
//		if (tag==event)
//		{
//			do
//			{
//				tag=gettag(f);
//				getline (f,data,'<');
//				if (tag!=endevent)
//					{
//						f2<<tag<<endl<<data<<endl<<endl;
//						if (tag=="DATE")
//							date=atoi(data.c_str());
//						if (tag=="DESCRIPTION")
//							description=data;
//					}
//			} while (tag!=endevent);
//			Event cur(date, description);
//			myEvents.push_back(cur);
//		}
//	}
//	f.close();
//	f2.close();
//}
//
//void XMLParser::readMovies()
//{
//}

